<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ptsp:ptsp:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ptsp:ptsp:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ptsp:ptsp:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ptsp:ptsp:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ptspList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="商品图片" align="center" prop="sptp" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.sptp" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="拼团名称" align="center" prop="ptmc" />
      <el-table-column label="原价" align="center" prop="yj" />
      <el-table-column label="拼团价" align="center" prop="ptj" />
      <el-table-column label="拼团人数" align="center" prop="ptrs" />
      <el-table-column label="参与人数" align="center" prop="xnctbprs" />
      <el-table-column label="限量" align="center" prop="xl" />
      <el-table-column label="成团数量" align="center" prop="ctsl" />
      <el-table-column label="限量剩余" align="center" prop="xlsy" />
      <el-table-column label="活动状态" align="center" prop="hdzt" >
        <template slot-scope="scope">
          {{scope.row.hdzt==1?'已结束':'进行中'}}
        </template>
      </el-table-column>
      <el-table-column label="拼团时间" align="center" prop="ptsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ptsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="sjzt" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.sjzt"
            active-color="#13ce66"
            active-text="正常"
            inactive-text="未开启"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ptsp:ptsp:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="cope(scope.row)"
            v-hasPermi="['ptsp:ptsp:edit']"
          >复制</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ptsp:ptsp:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-loading"
            @click="handlecount(scope.row)"
          >统计</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="CountDialog" width="1200px" append-to-body>
      <template>
        <el-table
          :data="count">
            <el-table-column label="id" align="center" prop="id" />
            <el-table-column label="商品图片" align="center" prop="sptp" width="100">
              <template slot-scope="scope">
                <image-preview :src="scope.row.sptp" :width="50" :height="50"/>
              </template>
            </el-table-column>
            <el-table-column label="拼团名称" align="center" prop="ptmc" />
            <el-table-column label="原价" align="center" prop="yj" />
            <el-table-column label="拼团价" align="center" prop="ptj" />
            <el-table-column label="拼团人数" align="center" prop="ptrs" />
            <el-table-column label="参与人数" align="center" prop="xnctbprs" />
            <el-table-column label="限量" align="center" prop="xl" />
            <el-table-column label="成团数量" align="center" prop="ctsl" />
            <el-table-column label="限量剩余" align="center" prop="xlsy" />
            <el-table-column label="活动状态" align="center" prop="hdzt" >
              <template slot-scope="scope">
                {{scope.row.hdzt==1?'已结束':'进行中'}}
              </template>
            </el-table-column>
            <el-table-column label="拼团时间" align="center" prop="ptsj" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.ptsj, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="上架状态" align="center" prop="sjzt" >
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.sjzt"
                  active-color="#13ce66"
                  active-text="正常"
                  inactive-text="未开启"
                  inactive-color="#ff4949"
                  :active-value="1"
                  :inactive-value="0">
                </el-switch>
              </template>
            </el-table-column>
        </el-table>
      </template>
    </el-dialog>
    <!-- 添加或修改拼团商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-steps :active="active" finish-status="success">
          <el-step title="步骤 1"></el-step>
          <el-step title="步骤 2"></el-step>
          <el-step title="步骤 3"></el-step>
        </el-steps>
        <div v-show="active==0||active==1" v-if="title=='添加拼团商品'">
          <el-form-item label="选择商品">
            <div @click="handleSpxx()">
              <image-upload style="pointer-events: none" v-model="imageShow"></image-upload>
            </div>
          </el-form-item>
        </div>
        <el-dialog
          append-to-body
          title="提示"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose">
          <template>
            <el-table
              :data="tableData"
              stripe
              style="width: 100%"
              @selection-change="handleSelectionProduct">
              <el-table-column
                type="selection"
                width="55">
              </el-table-column>
              <el-table-column
                prop="id"
                label="商品id"
                width="180">
              </el-table-column>
              <el-table-column
                prop="image"
                label="商品图片"
                width="180">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.image" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="productName"
                label="商品信息">
              </el-table-column>
            </el-table>
          </template>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="setFrom">确 定</el-button>
        </span>
        </el-dialog>
        <div v-if="this.active==1">
          <el-form-item label="商品图片" prop="sptp">
            <image-upload v-model="form.sptp"/>
          </el-form-item>
          <el-form-item label="原价" prop="yj">
            <el-input v-model="form.yj" placeholder="请输入原价" />
          </el-form-item>
          <el-form-item label="拼团名称" prop="ptmc">
            <el-input v-model="form.ptmc" placeholder="请输入拼团名称" />
          </el-form-item>
          <el-form-item label="拼团简介" prop="ptjj">
            <el-input type="textarea" v-model="form.ptjj"></el-input>
          </el-form-item>
          <el-form-item label="拼团时间" prop="ptsj">
            <el-date-picker clearable
                            v-model="form.ptsj"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择拼团时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="物流方式" prop="wlfs">
            <el-checkbox-group v-model="wlfs">
              <el-checkbox label="快递" ></el-checkbox>
              <el-checkbox label="到店核销" ></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="运费设置" prop="yfsz">
            <el-radio-group v-model="form.yfsz">
              <el-radio  :label="1" >固定邮费</el-radio>
              <el-radio  :label="2">运费模板</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="固定邮费" prop="gdyf" v-if="form.yfsz==1">
            <el-input v-model="form.gdyf" placeholder="请输入固定邮费" />
          </el-form-item>
          <el-form-item label="" prop="yfmb" v-if="form.yfsz==2">
            <el-select v-model="form.yfmb" placeholder="请选择">
              <el-option
                v-for="item in productListSelectName"
                :key="item.id"
                :label="item.productName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="拼团时效(单位小时)" prop="ptsx">
            <el-input v-model="form.ptsx" placeholder="请输入拼团时效(单位小时)" />
          </el-form-item>
          <el-form-item label="拼团价" prop="ptj">
            <el-input v-model="form.ptj" placeholder="请输入拼团价" />
          </el-form-item>
          <el-form-item label="限量" prop="xl">
            <el-input v-model="form.xl" placeholder="请输入限量" />
          </el-form-item>
          <el-form-item label="成团数量" prop="ctsl">
            <el-input v-model="form.ctsl" placeholder="请输入成团数量" />
          </el-form-item>
          <el-form-item label="限量剩余" prop="xlsy">
            <el-input v-model="form.xlsy" placeholder="请输入限量剩余" />
          </el-form-item>
          <el-form-item label="拼团人数" prop="ptrs">
            <el-input v-model="form.ptrs" placeholder="请输入拼团人数" />
          </el-form-item>
          <el-form-item label="虚拟成团补齐人数" prop="xnctbprs">
            <el-input v-model="form.xnctbprs" placeholder="请输入虚拟成团补齐人数" />
          </el-form-item>
          <el-form-item label="总购买数量限制" prop="zgmslxz">
            <el-input v-model="form.zgmslxz" placeholder="请输入总购买数量限制" />
          </el-form-item>
          <el-form-item label="单次购买数量限制" prop="dcgmslxz">
            <el-input v-model="form.dcgmslxz" placeholder="请输入单次购买数量限制" />
          </el-form-item>
          <el-form-item label="单位" prop="dw">
            <el-input v-model="form.dw" placeholder="请输入单位" />
          </el-form-item>
          <el-form-item label="团长返佣比例" prop="tzfybl">
            <el-input v-model="form.tzfybl" placeholder="请输入团长返佣比例" />
          </el-form-item>
          <el-form-item label="拼团是否参与分销" prop="ptsfcyfx">
            <el-switch
              style="display: block"
              v-model="form.ptsfcyfx"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              active-text="开启"
              inactive-text="关闭">
            </el-switch>
          </el-form-item>
          <el-form-item label="排序" prop="px">
            <el-input v-model="form.px" placeholder="请输入排序" />
          </el-form-item>
          <el-form-item label="热门推荐" prop="rmtj">
              <el-switch
                style="display: block"
                v-model="form.rmtj"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                active-text="开启"
                inactive-text="关闭">
              </el-switch>
          </el-form-item>
          <el-form-item label="活动状态" prop="sjzt">
              <el-switch
                style="display: block"
                v-model="form.sjzt"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                active-text="开启"
                inactive-text="关闭">
              </el-switch>
          </el-form-item>
          <el-form-item label="规格选择" prop="ggid">
          <template v-if="this.active==1">
            <el-table :data="tableDataGg">
              <el-table-column
                type="selection"
                width="55">
                <template slot-scope="scope">
                  <input type="radio"  v-model="form.ggid" :value="scope.row.id"   @click="fgid(scope.row)"></input>
                </template>
              </el-table-column>
              <el-table-column label="尺码" align="center" prop="cm" />
              <el-table-column label="颜色分类" align="center" prop="color" />
              <el-table-column label="图片" align="center" prop="tp" >
                <template slot-scope="scope">
                  <image-preview :src="scope.row.tp" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column label="库存" align="center" prop="kc" />
              <el-table-column label="限量" align="center" prop="xl" >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.xl" placeholder="请输入限量" />
                </template>
              </el-table-column>
              <el-table-column label="重量" align="center" prop="zl" />
              <el-table-column label="体积" align="center" prop="tj" />
            </el-table>
          </template>
          </el-form-item>
        </div>
        <div v-if="this.active==2">
          <el-form-item label="详情" prop="xq">
            <editor v-model="form.xq" :min-height="192" v-html="form.xq"/>
          </el-form-item>
        </div>
      </el-form>
      <el-button style="margin-top: 12px;" @click="shang">上一步</el-button>
      <el-button style="margin-top: 12px;" @click="next">下一步</el-button>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPtsp, getPtsp, delPtsp, addPtsp, updatePtsp } from "@/api/ptsp/ptsp";
import {productList} from "@/api/comments/comments";
import {ggList} from "@/api/integral/integral";
import {getKjsp} from "@/api/kjgl/kjsp";

export default {
  name: "Ptsp",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      active:0,
      // 非多个禁用
      multiple: true,
      dialogVisible:false,
      imageShow:null,
      // 显示搜索条件
      showSearch: true,
      tableDataGg:[],
      tableData:[],
      count:[],
      // 总条数
      total: 0,
      CountDialog:false,
      // 拼团商品表格数据
      ptspList: [],
      productListSelectName:[],
      wlfs:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      listGg:[],
      multipleSelection:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        sjzt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getSelectProduct();
    this.selectGg();
  },
  methods: {
    CountDialog(){

    },
    cope(row){
      this.active=1;
      this.reset();
      const id = row.id || this.ids
      getPtsp(id).then(response => {
        this.form = response.data;
        this.form.wlfs = this.form.wlfs.split(",");
        this.form.id=null;
        this.open = true;
        this.title = "复制拼团商品";
      });
    },
    handlecount(row){
      this.$router.push({ path: "/ptgl/tj", query: { id: row.id} })
    },
    fgid(row){
      this.form.ggid=row.id;
      this.form.xl=row.xl;
    },
    shang(){
      if (this.active-- > 0) this.active = this.active--;
    },
    next() {
      if (++this.active > 2) this.active = 0;
    },
    getSelectProduct(){
      productList().then(res=>{
        this.tableData=res;
        this.productListSelectName=res
      })
    },
    selectGg(){
      ggList(null).then(res=>{
        this.tableDataGg=res;
      })
    },
    handleSelectionProduct(val){
      this.multipleSelection = val
      if (val.length > 1) {
        this.$refs.multipleTableFrag.clearSelection()
        this.$refs.multipleTableFrag.toggleRowSelection(val.pop())
      }
      console.log(val[0]['name'])
    },
    handleClose(){
      this.$confirm('确认关闭？')
        .then(_ => {
          this.dialogVisible=false
        })
        .catch(_ => {});
    },
    /** 查询拼团商品列表 */
    getList() {
      this.loading = true;
      listPtsp(this.queryParams).then(response => {
        this.ptspList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sptp: null,
        ptmc: null,
        ptjj: null,
        ptsj: null,
        wlfs: [],
        yfsz: null,
        gdyf: null,
        yfmb: null,
        ptsx: null,
        ptrs: null,
        xnctbprs: null,
        zgmslxz: null,
        dcgmslxz: null,
        dw: null,
        tzfybl: null,
        ptsfcyfx: null,
        px: null,
        rmtj: null,
        hdzt: null,
        ptj: null,
        xl: null,
        ggid: null,
        xq: null,
        yj: null,
        ctsl: null,
        xlsy: null,
        sjzt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleSpxx(){
      this.dialogVisible=true;
    },
    handleSelectionProductGg(val){
      this.listGg = val
      try {
        // 确保 this.listGg 是数组且不为空
        if (Array.isArray(this.listGg) && this.listGg.length > 0) {
          console.log(this.listGg[0]); // 输出数组中的第一个元素
        } else {
          console.log("this.listGg is undefined or empty.");
        }
      } catch (error) {
        console.error("Error accessing properties:", error);
      }
      this.form.xl=this.listGg[0].xl;
      this.form.status=this.form.productStatus;
      console.log(val)
    },
    setFrom(){
      this.form.sptp=this.multipleSelection[0].image;
      this.form.ptmc=this.multipleSelection[0].productName;
      this.form.dw=this.multipleSelection[0].unit;
      this.form.px=this.multipleSelection[0].sort;
      this.imageShow=this.multipleSelection[0].image;
      this.form.xq='<img src="'+this.multipleSelection[0].image+'">';
      this.dialogVisible=false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.active=0;
      this.reset();
      this.open = true;
      this.title = "添加拼团商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.active=1;
      this.reset();
      const id = row.id || this.ids
      getPtsp(id).then(response => {
        this.form = response.data;
        this.form.wlfs = this.form.wlfs.split(",");
        this.open = true;
        this.title = "修改拼团商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.wlfs=this.wlfs.join(",");
          this.form.xq=this.form.sptp;
          if (this.form.id != null) {
            updatePtsp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.xq=this.form.sptp;
            addPtsp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除拼团商品编号为"' + ids + '"的数据项？').then(function() {
        return delPtsp(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ptsp/ptsp/export', {
        ...this.queryParams
      }, `ptsp_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
